/**
 * 
 */
package za.co.sindi.oauth.server.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Buhake Sindi
 * @since 26 March 2012
 *
 */
public class ModuleConfig implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3304867121538560154L;
	private List<ServiceConfig> serviceConfigList;
	
	/**
	 * Add a {@link ServiceConfig} to the Service Config List.
	 * 
	 * @param serviceConfig
	 */
	public void addServiceConfig(ServiceConfig serviceConfig) {
		if (serviceConfigList == null) {
			serviceConfigList = new ArrayList<ServiceConfig>();
		}
		
		if (serviceConfig != null) {
			serviceConfigList.add(serviceConfig);
		}
	}
	
	/**
	 * Returns an array of {@link ServiceConfig}.
	 * @return
	 */
	public ServiceConfig[] getServiceConfigs() {
		if (serviceConfigList == null) {
			return null;
		}
		
		return serviceConfigList.toArray(new ServiceConfig[serviceConfigList.size()]);
	}
	
	/**
	 * Returns a {@link ServiceConfig} based on the URI path provided.
	 * @param path
	 * @return a {@link ServiceConfig}, if found, <code>null</code> otherwise.
	 */
	public ServiceConfig getServiceConfigByPath(String path) {
		if (serviceConfigList != null) {
			for (ServiceConfig serviceConfig : serviceConfigList) {
				if (path.equals(serviceConfig.getPath())) {
					return serviceConfig;
				}
			}
		}
		
		return null;
	}
	
	public boolean serviceConfigExistsByPath(String path) {
		return getServiceConfigByPath(path) != null;
	}
	
	/**
	 * Get all {@link ServiceConfig} found with the <code>serviceName</code>.
	 * @param serviceName
	 * @return
	 */
	public ServiceConfig[] getServiceConfigByName(String serviceName) {
		if (serviceConfigList == null) {
			return null;
		}
		
		List<ServiceConfig> matchedList = new ArrayList<ServiceConfig>();
		for (ServiceConfig serviceConfig : serviceConfigList) {
			if (serviceName.equals(serviceConfig.getName())) {
				matchedList.add(serviceConfig);
			}
		}
		
		return matchedList.toArray(new ServiceConfig[matchedList.size()]);
	}
}
